package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.RecordAccept;
import com.yqg.protection.mapper.RecordAcceptMapper;
import com.yqg.protection.service.IRecordAcceptService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * RecordAcceptController 前端控制器
 * @author 小宇
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/record-accept")
public class RecordAcceptController {

    @Resource
    private IRecordAcceptService recordAcceptService;

    @Autowired
    private RecordAcceptMapper recordAcceptMapper;
    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @SysLog("新增或者修改受理记录")
    public Result save(@RequestBody RecordAccept recordAccept) {
        recordAccept.setIsDelete(0);
        return Result.success(recordAcceptService.saveOrUpdate(recordAccept));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @SysLog("删除单个受理记录")
    public Result delete(@PathVariable Integer id) {
        RecordAccept recordAccept = recordAcceptService.getById(id);
        recordAccept.setIsDelete(1);
        return Result.success(recordAcceptService.saveOrUpdate(recordAccept));
    }
    @PostMapping("/deleteByIdList")
    @SysLog("删除多个受理记录")
    public Result deleteByIdList(@RequestBody List<Long> id) {
        List<RecordAccept> recordAccepts = recordAcceptService.listByIds(id);
        for (RecordAccept recordAccept:recordAccepts) {
            recordAccept.setIsDelete(1);
        }
        return Result.success(recordAcceptService.saveOrUpdateBatch(recordAccepts));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<RecordAccept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<RecordAccept> list=  recordAcceptService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(recordAcceptService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String keyWord,
                           String startTime,
                           String endTime,
                           Integer level,
                           Long organizationId,
                           String isAllClient,
                           @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<RecordAccept> recordAccepts = recordAcceptMapper.findPage(pageNum, pageSize, keyWord, startTime, endTime, level, organizationId, isAllClient, clientIds);
        Long count = recordAcceptMapper.findPageCount(keyWord, startTime, endTime, level, organizationId, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", recordAccepts);
        map.put("total", count);
        return Result.success(map);
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String keyWord,
                       String startTime,
                       String endTime,
                       Integer level,
                       Long organizationId,
                       String isAllClient,
                       @RequestParam List<Long> clientIds) {
        recordAcceptService.export(response, keyWord, startTime, endTime, level, organizationId, isAllClient, clientIds);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入受理记录")
    public void importData(MultipartFile file) {
        recordAcceptService.importData(file);
    }
}

